Repository navigation
net/netforward: Add receiver-aware multicast forwarding (stage 1). - #22839
Brijesh-Thakkar wants to merge 7 commits into
Conversation
Multicast packets are currently forwarded through every network device, no matter whether a receiver is reachable through it. Add the options that will select receiver-aware multicast forwarding: * NET_IPFORWARD_MULTICAST enables the multicast forwarding table. It depends on NET_IPFORWARD_BROADCAST because it filters the devices that this option forwards multicast packets to. It is disabled by default, so the current behavior does not change. * NET_IPFORWARD_MULTICAST_NENTRIES sets the size of the statically allocated table. The options do not select any code yet, the table and its use in the forwarding path follow in the next commits. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
|
| * | ||
| ****************************************************************************/ | ||
|
|
||
| int netfwd_mcast_ioctl(int cmd, unsigned long arg) |
There was a problem hiding this comment.
why not keep in netdev_ioctl as the static function like others
There was a problem hiding this comment.
depends on the interface question below. with socket options the handler moves to ipv4_setsockopt.c/ipv6_setsockopt.c and this file goes away, if we keep the ioctl i'll make it a static function in netdev_ioctl.c
There was a problem hiding this comment.
yes, either way is fine.
|
|
||
| /* Multicast forwarding table ***********************************************/ | ||
|
|
||
| #define SIOCADDMCFWD _SIOC(0x0049) /* Add an egress interface to the |
There was a problem hiding this comment.
could we use Linux/POSIX compliant interface?
#define IP_ADD_MEMBERSHIP (__SO_PROTOCOL + 4) /* Join a multicast group */
#define IP_DROP_MEMBERSHIP (__SO_PROTOCOL + 5) /* Leave a multicast group */
#define IP_UNBLOCK_SOURCE (__SO_PROTOCOL + 6) /* Unblock previously
* blocked multicast source */
#define IP_BLOCK_SOURCE (__SO_PROTOCOL + 7) /* Stop receiving multicast
* data from source */
#define IP_ADD_SOURCE_MEMBERSHIP (__SO_PROTOCOL + 8) /* Join a multicast group;
* allow receive only from
* source */
#define IP_DROP_SOURCE_MEMBERSHIP (__SO_PROTOCOL + 9) /* Leave a source-specific
* group. Stop receiving
* data from a given
* multicast group that come
* from a given source */
#define IP_MSFILTER (__SO_PROTOCOL + 10) /* Access advanced, full-
* state filtering API */
#define IP_MULTICAST_ALL (__SO_PROTOCOL + 11) /* Modify the delivery policy
* of multicast messages bound
* to INADDR_ANY */
/* SOL_IPV6 protocol-level socket options. */
#define IPV6_JOIN_GROUP (__SO_PROTOCOL + 1) /* Join a multicast group */
#define IPV6_LEAVE_GROUP (__SO_PROTOCOL + 2) /* Quit a multicast group */
#define IPV6_MULTICAST_HOPS (__SO_PROTOCOL + 3) /* Multicast hop limit */
#define IPV6_MULTICAST_IF (__SO_PROTOCOL + 4) /* Interface to use for
* outgoing multicast packets */
#define IPV6_MULTICAST_LOOP (__SO_PROTOCOL + 5) /* Multicast packets are
* delivered back to the local
* application */
/* Protocol-independent multicast options (RFC 3678) for the level
* IPPROTO_IP or IPPROTO_IPV6, with the values of Linux.
*/
#define MCAST_JOIN_GROUP (__SO_PROTOCOL + 31) /* group_req */
#define MCAST_BLOCK_SOURCE (__SO_PROTOCOL + 32) /* group_source_req */
#define MCAST_UNBLOCK_SOURCE (__SO_PROTOCOL + 33) /* group_source_req */
#define MCAST_LEAVE_GROUP (__SO_PROTOCOL + 34) /* group_req */
#define MCAST_JOIN_SOURCE_GROUP (__SO_PROTOCOL + 35) /* group_source_req */
#define MCAST_LEAVE_SOURCE_GROUP (__SO_PROTOCOL + 36) /* group_source_req */
There was a problem hiding this comment.
the linux interface for this is the multicast routing api (linux/mroute.h, mroute6.h): MRT_INIT on a raw igmp socket, then MRT_ADD_VIF / MRT_ADD_MFC, i.e. the kernel mfc that smcroute/mrouted/pimd work with. i'd implement a minimal subset: vifs by ifindex, (,G) entries via MRT_ADD_MFC_PROXY with a zero origin, mapped onto the current table, and drop SIOCADDMCFWD/SIOCDELMCFWD. ipv4 needs a raw igmp socket first, since nuttx only has raw icmp/icmpv6 ip sockets today, so that would be a separate prep pr.
or did you mean socket options with struct group_req like MCAST_JOIN_GROUP? that's smaller, but it would be a nuttx-only option.
note that linux (,G) entries also check the interface the packet arrived on, today's table accepts any incoming interface, so the mrt option would add that check.
which one do you prefer? apps#4287 is already merged, so the test app would get a follow-up pr either way.
There was a problem hiding this comment.
yes, it's better to align with linux userspace contract, we can implement the minimal feature what we want now and improve the functionality step by step.
There was a problem hiding this comment.
great, will do. plan: a small prep pr adding raw IPPROTO_IGMP sockets (needed for MRT_INIT), then this pr switches from SIOCADDMCFWD/SIOCDELMCFWD to a minimal MRT_/MRT6_ subset, then a follow-up apps pr for the test app (also for the kconfig rename). i'll convert this pr to draft until then.
Add the table that will tell the forwarding path which network devices a multicast packet must be forwarded through. Each entry is a (*,G) group with a bitmap of egress interface indices and an optional expiry time. * The table is a static array of CONFIG_NET_IPFORWARD_MULTICAST_NENTRIES entries, searched linearly. There is no dynamic allocation. * netfwd_mcast_join() and netfwd_mcast_leave() add and remove one egress interface of a group. Rejecting interface indices outside of 1..MAX_IFINDEX keeps every index inside the 32 bit bitmap. * netfwd_mcast_lookup() returns the egress bitmap of a group, or zero if the group has no entry or the entry has expired. * Expiry is checked on lookup and expired slots are reused by the next join, so no timer or work item is needed. * netfwd_mcast_ifdown() clears a device from every entry. The table has its own mutex, private to netfwd_mcast.c and taken only by the functions above. It is a leaf lock: nothing is called while it is held. The packet path, which holds the lock of the receiving device but not the network lock, can therefore look up the table safely while the table is changed from another context. The table is built only when NET_IPFORWARD_MULTICAST is selected and nothing uses it yet. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
4ab48f0 to
b0e3e4e
Compare
| @@ -376,23 +376,57 @@ static int ipv4_in(FAR struct net_driver_s *dev) | |||
| } | |||
| #endif | |||
|
|
|||
| #ifdef CONFIG_NET_IPFORWARD_BROADCAST | |||
There was a problem hiding this comment.
·#ifdef CONFIG_NET_IPFORWARD_BROADCAST
if (!forwarded)
#endif
#ifdef CONFIG_NET_STATISTICS
g_netstats.ipv4.drop++;
#endif
There was a problem hiding this comment.
done. i put braces around the counter though: with NET_STATISTICS off (the default) the bare if (!forwarded) would guard the goto drop below it, and a forwarded packet would fall through to local input.
| * which only decide if the packet is delivered locally. | ||
| */ | ||
|
|
||
| forwarded = ipv4_forward_broadcast(dev, ipv4); |
There was a problem hiding this comment.
move after line 418 and revert the change at line 411-412
| * it is not a dropped packet. | ||
| */ | ||
|
|
||
| if (forwarded) |
There was a problem hiding this comment.
·#ifdef CONFIG_NET_IPFORWARD_BROADCAST
if (!forwarded)
#endif
#ifdef CONFIG_NET_STATISTICS
g_netstats.ipv4.drop++;
#endif
There was a problem hiding this comment.
done, same braces. the guard here is NET_IPFORWARD_MULTICAST since forwarded only exists with it.
| @@ -70,6 +70,37 @@ config NET_IPFORWARD_BROADCAST | |||
| If selected, broadcast packets received on one network device will | |||
| be forwarded though other network devices. | |||
|
|
|||
| config NET_IPFORWARD_MCAST | |||
There was a problem hiding this comment.
| config NET_IPFORWARD_MCAST | |
| config NET_IPFORWARD_MULTICAST |
align with NET_IPFORWARD_BROADCAST
| through all network devices as before, and no additional code or | ||
| data is built. | ||
|
|
||
| config NET_IPFORWARD_MCAST_NENTRIES |
There was a problem hiding this comment.
| config NET_IPFORWARD_MCAST_NENTRIES | |
| config NET_IPFORWARD_MUTLICAST_NENTRIES |
There was a problem hiding this comment.
done, used NET_IPFORWARD_MULTICAST_NENTRIES (assuming MUTLICAST was a typo)
With NET_IPFORWARD_MULTICAST, an IPv4 multicast packet is forwarded only through the network devices that the multicast forwarding table lists for its group. A group without an entry is not forwarded. * ipv4_forward_broadcast() looks the group up once, removes the receiving device from the result and returns before walking the devices when no egress interface is left. The bitmap is passed to the per device callback in a new struct ipv4_fwdarg_s, private to ipv4_forward.c. The callback only tests the bit of its device, after its existing check that excludes the receiving device, so a packet can never go back out the interface it came in on. * ipv4_forward_broadcast() now always returns true if the packet was forwarded through at least one device, with or without NET_IPFORWARD_MULTICAST. The callback records it in struct ipv4_fwdarg_s, which it now always receives instead of the device. * Broadcast packets are not multicast and are still forwarded through all devices. The other filters are unchanged: source device flags, link local 224.0.0.0/24, and TTL. * ipv4_in() now also forwards a multicast packet whose group was not joined on the receiving device, because the forwarding decision depends on the egress devices, not on the joined groups. Local delivery is still gated by the joined groups. A packet that was forwarded but is not for this host is released without counting a drop. * ipv4_in() does the same for a broadcast packet: if it was forwarded and no protocol takes it, it is released without counting a drop. Without NET_IPFORWARD_MULTICAST, the only change in behavior is that last one: a forwarded broadcast packet that is not UDP (or with no UDP broadcast support) is no longer counted in the IPv4 drop statistics. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
With NET_IPFORWARD_MULTICAST, an IPv6 multicast packet is forwarded only through the network devices that the multicast forwarding table lists for its group. A group without an entry is not forwarded. ipv6_forward_broadcast() looks the group up once, removes the receiving device from the result and returns before walking the devices when no egress interface is left. The bitmap is passed to the per device callback in a struct ipv6_fwdarg_s, private to ipv6_forward.c, like it is for IPv4. The callback only tests the bit of its device, after its existing check that excludes the receiving device, so a packet can never go back out the interface it came in on. The other filters are unchanged: source device flags, the reserved, interface-local and link-local scopes, and the hop limit. Like ipv4_forward_broadcast(), ipv6_forward_broadcast() now always returns true if the packet was forwarded through at least one device, with or without NET_IPFORWARD_MULTICAST. ipv6_in() does not need a change and does not use the result. It has no check of the groups that were joined on the receiving device before it forwards, and it does not count a packet as dropped after it was forwarded. Without NET_IPFORWARD_MULTICAST, the behavior is not changed. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
…register. The multicast forwarding table identifies the egress devices by their interface index. Remove a device from every entry of the table when it goes down, so a packet is not forwarded through a device that is not running and the entry is not revived when the device comes back up. Also remove it when the device is unregistered, because its interface index can then be assigned to another device, which must not inherit the entries of the old one. netdev_unregister() clears the table with the device list lock held, before the interface index is freed. The table lock is a leaf lock, so it can be taken there. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
Add SIOCADDMCFWD and SIOCDELMCFWD to add and remove an egress interface of a multicast group, so that the forwarding table can be configured from user space. The argument is a struct netfwd_mcastreq_s with the group (IPv4 or IPv6), the name of the interface and, to add, the lifetime of the entry in seconds, where zero means no expiry. * A group that is not a multicast address is refused with -EINVAL, an unknown interface with -ENODEV and a full table with -ENOSPC. * The lifetime is converted to ticks with SEC2TICK() on a clock_t. A lifetime for which the conversion could overflow is refused. * The interface name is resolved and the table updated with the device list locked, so the device cannot be unregistered in between and leave its interface index in the table. The command handling is in its own file and is chained into the socket ioctl handler like the bridge commands. It is the only user space interface of the table, so it can be replaced, for example by MRT_* socket options, without changing the table or the forwarding path. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
Document CONFIG_NET_IPFORWARD_MULTICAST: how multicast packets are forwarded through the egress interfaces of the multicast forwarding table, what is unchanged, the size and lifetime of the table entries, and the SIOCADDMCFWD and SIOCDELMCFWD ioctl commands that configure the table. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Brijesh Thakkar <brijeshthakkariitian5126@gmail.com>
b0e3e4e to
fb28e04
Compare
Note: Please adhere to Contributing Guidelines.
Summary
Related to #18762 (stage 1 of 4).
Related: #22803. #22831 changes the same broadcast loop in ipv4_forward.c/ipv6_forward.c; whichever merges second needs a small rebase (resolution already prepared).
Why. With
CONFIG_NET_IPFORWARD_BROADCAST, a multicast packet isforwarded through every other network device, whether or not a receiver is
reachable through it. IPv4 additionally only forwards groups that were
joined on the receiving device, and IPv6 forwards every multicast packet
above link-local scope. #18762 proposes receiver-aware forwarding. This PR
is stage 1: the forwarding table, the data path that uses it and a minimal
control interface. Learning the table from IGMP (stage 2) and MLD (stage 3)
snooping and a sim defconfig (stage 4) will follow as separate PRs.
What. A new option
CONFIG_NET_IPFORWARD_MULTICAST(defaultn, dependson
NET_IPFORWARD_BROADCAST, selectsNETDEV_IFINDEX):the multicast forwarding table lists for its group. A group without an
entry is not forwarded.
(
IFF_NOSRC_FORWARD/IFF_NODST_FORWARD, 224.0.0.0/24, IPv6 scopes ff0xfor x <= 2, TTL/hop limit) still apply.
the IPv4 statistics, see Compatibility below.
How.
net/netforward/netfwd_mcast.c: a static table ofCONFIG_NET_IPFORWARD_MULTICAST_NENTRIES(default 8) (*,G) entries, each agroup address, a
uint32_tegress interface-index bitmap (indices 1..32,others are refused) and an optional expiry checked lazily on lookup.
API:
netfwd_mcast_join(),netfwd_mcast_leave(),netfwd_mcast_lookup(),netfwd_mcast_ifdown(). No timers, work itemsor dynamic allocation.
mutex_t, private tonetfwd_mcast.cand taken only inside those four functions, with nothing called while it
is held (a leaf lock, like
g_ipfilter_lockinnet/ipfilter). Thepacket path does not take the network lock. The ioctl resolves the
interface and updates the table with the device list locked, and
netdev_unregister()clears the device from the table under the samelock, so an entry cannot outlive its device.
ipv4_forward_broadcast()/ipv6_forward_broadcast()look the groupup once, remove the receiving device, return early if nothing is left and
pass the bitmap to the per-device callbacks, which only test one bit after
their existing receiving-device check. Both functions now have a single
prototype that returns
bool: true if the packet was forwarded through atleast one device, with or without the option. The callbacks always get a
small struct private to each file (
ipv4_fwdarg_s,ipv6_fwdarg_s) andrecord it there.
ipv4_in()now also forwards a multicast packet whose group was notjoined on the receiving device (forwarding depends on the egress devices;
local delivery is still gated by IGMP). A forwarded packet that is not
for this host is released without being counted as dropped, and the same
now applies to a forwarded broadcast packet that no protocol takes.
ipv6_in()needed no change.netdev_ifdown()andnetdev_unregister()remove the device from thetable, so it is not revived on ifup and a reused interface index does not
inherit entries.
SIOCADDMCFWD/SIOCDELMCFWD(struct netfwd_mcastreq_sininclude/nuttx/net/netfwd_mcast.h) add or remove one egress interface of agroup, with a lifetime in seconds (0 = permanent). The handler is isolated
in
net/netforward/netfwd_mcast_ioctl.cso it can later be replaced byMRT_*-style socket options without touching the table or data path.Documentation/os/networking/ipforward_mcast.rst.Test program: apache/nuttx-apps
examples/ipfwdmcast(apache/nuttx-apps#4287).The merged test program still uses the old option name
NET_IPFORWARD_MCAST;the results below were made with it renamed locally, and a follow-up
nuttx-apps PR will make the same change.
Impact
New feature, disabled by default. Nothing changes unless
CONFIG_NET_IPFORWARD_MULTICAST=y; with it, multicast is no longer flooded butforwarded per the table (which starts empty).
New Kconfig options
NET_IPFORWARD_MULTICAST,NET_IPFORWARD_MULTICAST_NENTRIES;new ioctls
SIOCADDMCFWD(0x0049) andSIOCDELMCFWD(0x004A), after thelast CAN ioctl; new header
include/nuttx/net/netfwd_mcast.h. No existingAPI changes.
Build: Make and CMake updated for the two new files.
Size,
imxrt1050-evk:netnshwith IPv4, IPv6, IGMP, MLD andforwarding, all built in the same directory (text / data / bss):
Documentation: added.
Security: the ioctls configure forwarding like the existing route ioctls.
Compatibility: with the option off, forwarding is unchanged, but there
is one deliberate change in the IPv4 statistics. A broadcast packet that
was forwarded through at least one device, and that no protocol takes
(not UDP, or no UDP broadcast support), is no longer counted in
g_netstats.ipv4.drop, the same as a forwarded multicast packet with theoption on. Before this PR it was counted as dropped even though it was
forwarded. IPv6 statistics are unchanged. The +72 bytes of text with
the option off are the
boolresults and this check.Testing
Host: Ubuntu 26.04 LTS, Linux 7.0.0-44-generic x86_64, GCC 15.2.0,
arm-none-eabi-gcc 14.2.1, CMake 4.2.3 / Ninja 1.13.2. Branch based on
master 97d6bc7.
Builds (no warnings except the board's own
#warnings on imxrt):on (
sim:nsh+NET, IPv4, IPv6, UDP, TCP, IGMP, MLD, ICMPv6,NET_BROADCAST,NET_STATISTICS,NET_IPFORWARD,NET_IPFORWARD_BROADCAST): 14/14 builds, 0 warnings.imxrt1050-evk:netnshwith the same network options, option off and on(sizes above).
checkpatch.sh -g: clean for every commit.Runtime, sim, three TUN devices (test app
examples/ipfwdmcast, no hostsetup needed; packets are injected on tun0, copies sent out of tun0/tun1/tun2
are counted).
Option off (existing behavior):
Option on (50 checks, trimmed):
The "table changed while packets are forwarded" checks inject 120 IPv4 and
IPv6 packets (plus 30 for a group without an entry) while another thread
keeps adding and removing tun2 and other groups with the ioctls; tun1 stays
listed throughout and must get every packet. The same suite also passes
50/50 on
sim:smp(4 CPUs), where the ioctls and the packet path really runin parallel (tun1 120/120, tun2 66/120, 145 rounds of ioctls, no errors),
with
DEBUG_ASSERTIONSenabled in all runs.The test also catches regressions of the cleanup: with the two
netfwd_mcast_ifdown()calls removed, exactly the related checks fail:Runtime, sim, three TAP devices with host listeners (option off: flood;
option on: no entry = nothing, entry = listed interfaces only, expiry and
ifdown stop forwarding):
Host: as above, iproute2 6.19.0, Python 3.14.4. The sim binaries (built
from this branch, option off and on,
SIM_NETDEV_NUMBER=3, same networkoptions as above plus
NET_SOCKOPTS,NET_TUNandEXAMPLES_IPFWDMCAST)run as root in network namespaces; tap1 and tap2 are
moved to their own namespaces with one listener each, and 3 IPv4 and 3 IPv6
packets per case are sent from tap0. Counts are (IPv4, IPv6) packets
received behind tap1 and tap2.